package edu.chl.jadetalk.control;

import edu.chl.jadetalk.business.MailerBeanLocal;
import edu.chl.jadetalk.business.UserManagerBeanLocal;
import edu.chl.jadetalk.view.RegisterBackingBean;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import edu.chl.jadetalk.core.User;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author jesper, Daniel
 */
@ManagedBean
@RequestScoped
public class RegisterControllerBean {

    @ManagedProperty(value="#{registerBackingBean}")
    private RegisterBackingBean registerBackingBean;

    @EJB
    private UserManagerBeanLocal userManagerBean;
    
    @EJB
    private MailerBeanLocal mailerBean;
    
    public String register(){
        
        // Check username
        if (registerBackingBean.getName().equals("")) {
            registerBackingBean.setMessage("You must enter a username.");
            return "onRegisterFail";
        } 
        for (User u : userManagerBean.listUsers()) {
            if (u.getUsername().equals(registerBackingBean.getName())) {
                registerBackingBean.setMessage("Username already exists.");
                return "onRegisterFail";
            }
        }
        
        // Check passwords
        if (registerBackingBean.getPassword().equals("")) {
            registerBackingBean.setMessage("You must enter a password.");
            return "onRegisterFail";
        } else if (!registerBackingBean.getPassword().equals(registerBackingBean.getConfirmedPassword())) {
            registerBackingBean.setMessage("Passwords do not match.");
            return "onRegisterFail";
        }
        
        // Check email
        Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
        Matcher m = p.matcher(registerBackingBean.getEmail());
        if (!m.matches()) {
            registerBackingBean.setMessage("Invalid email address.");
            return "onRegisterFail";
        }
        
        
        // Create user
        User user = userManagerBean.createUser(registerBackingBean.getName(), 
                registerBackingBean.getPassword(), 
                registerBackingBean.getEmail());
        
        // If user was created successfully, send confirmation mail
        if (user != null) {
            mailerBean.sendMail(user.getEmail(), "Registering successful!", "Congratultaions, you are now registered at Jadetalk!");
            registerBackingBean.setMessage("You are now registered! A confirmation mail will arrive soon.");
            return "onRegister";
        } else {
            registerBackingBean.setMessage("Could not register.");
            return "onRegisterFail";
        }
    }
    
    
    
    public RegisterBackingBean getRegisterBackingBean() {
        return registerBackingBean;
    }

    public void setRegisterBackingBean(RegisterBackingBean registerBackingBean) {
        this.registerBackingBean = registerBackingBean;
    }

    public UserManagerBeanLocal getUserManagerBean() {
        return userManagerBean;
    }

    public void setUserManagerBean(UserManagerBeanLocal userManagerBean) {
        this.userManagerBean = userManagerBean;
    }
	
}
